package com.magic.smartcity.view.more.models

import com.magic.smartcity.beans.*
import com.magic.smartcity.beans.pojo.ResultData
import com.magic.smartcity.utils.HttpClient
import com.magic.smartcity.view.more.models.services.HospitalService
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
import io.reactivex.rxjava3.internal.operators.maybe.MaybeDoAfterSuccess
import io.reactivex.rxjava3.schedulers.Schedulers

/**
 * @description:
 * @create: Create by Magic on 2022/2/19.
 */
class HospitalModel {
    private val TAG: String = "HospitalModel"
    val service = HttpClient().retrofit.create(HospitalService::class.java)

    /**
     * 获取指定医院轮播图
     */
    fun getHospitalBanner(
        hospitaLId: Int,
        success: (List<HospitalBanner>) -> Unit,
        error: (String) -> Unit
    ) {
        service.getHospitalBanner(hospitaLId).observeOn(Schedulers.io())
            .subscribeOn(AndroidSchedulers.mainThread())
            .doOnError { it.printStackTrace() }
            .subscribe {
                if (it.code == 200) {
                    success(it.data)
                } else {
                    error(it.msg)
                }
            }
    }

    /**
     * 获取医院列表
     */
    fun getHospitalList(success: (List<HospitalDetail>) -> Unit, error: (String) -> Unit) {
        service.getHospitalList().observeOn(Schedulers.io())
            .subscribeOn(AndroidSchedulers.mainThread())
            .doOnError { it.printStackTrace() }
            .subscribe {
                if (it.code == 200) {
                    success(it.rows)
                } else {
                    error(it.msg)
                }
            }
    }

    /**
     * 获取指定医院根据Id
     */
    fun getHospitalDetailById(
        hospitalId: Int,
        success: (HospitalDetail) -> Unit,
        error: (String) -> Unit
    ) {
        service.getHospitalById(hospitalId).subscribeOn(Schedulers.io())
            .subscribeOn(AndroidSchedulers.mainThread())
            .doOnError { it.printStackTrace() }
            .subscribe {
                if (it.code == 200) {
                    success(it.data)
                } else {
                    error(it.msg)
                }
            }
    }

    /**
     * 获取当前用户的就诊人卡片
     */
    fun getHospitalPatientCardList(
        success: (List<HospitalPatientCard>) -> Unit,
        error: (String) -> Unit
    ) {
        service.getPatientCardList().observeOn(Schedulers.io())
            .subscribeOn(AndroidSchedulers.mainThread())
            .doOnError { it.printStackTrace() }
            .subscribe {
                if (it.code == 200) {
                    success(it.rows)
                } else {
                    error(it.msg)
                }
            }
    }

    /**
     * 添加就诊人卡片
     */
    fun addPatientCard(
        hospitalPatientCard: HospitalPatientCard,
        success: (String) -> Unit,
        error: (String) -> Unit
    ) {
        service.addPatientCard(hospitalPatientCard).observeOn(Schedulers.io())
            .subscribeOn(AndroidSchedulers.mainThread())
            .doOnError {
                it.printStackTrace()
            }
            .subscribe {
                if (it.code == 200) {
                    success(it.msg)
                } else {
                    error(it.msg)
                }
            }
    }

    /**
     * 修改就诊人卡片信息
     */
    fun editPatientCard(
        hospitalPatientCard: HospitalPatientCard,
        success: (String) -> Unit,
        error: (String) -> Unit
    ) {
        service.editPatientCard(hospitalPatientCard)
            .observeOn(Schedulers.io())
            .subscribeOn(AndroidSchedulers.mainThread())
            .doOnError {
                it.printStackTrace()
            }
            .subscribe {
                if (it.code == 200) {
                    success(it.msg)
                } else {
                    error(it.msg)
                }
            }
    }

    /**
     * 获得门诊挂科列表
     */
    fun getDepartments(success: (List<HospitalDepartments>) -> Unit, error: (String) -> Unit) {
        service.getDepartments()
            .doOnError { it.printStackTrace() }
            .subscribe {
                if (it.code == 200) {
                    success(it.rows)
                } else {
                    error(it.msg)
                }
            }
    }

    /**
     * 根据类型ID获得门诊挂科列表
     */
    fun getDepartments(
        typeId: Int,
        success: (List<HospitalDepartments>) -> Unit,
        error: (String) -> Unit
    ) {
        service.getDepartmentsByTypeId(typeId)
            .doOnError { it.printStackTrace() }
            .subscribe {
                if (it.code == 200) {
                    success(it.rows)
                } else {
                    error(it.msg)
                }
            }
    }

    /**
     * 提交预约单
     */
    fun submitPatientReserve(hospitalPatientReserve: HospitalPatientReserve,success: (String) -> Unit,error: (String) -> Unit){
        service.submitPatientReserve(hospitalPatientReserve)
            .doOnError{it.printStackTrace()}
            .subscribe{
                if (it.code==200) {
                    success(it.msg)
                }else{
                    error(it.msg)
                }
            }
    }


}